﻿using Domain.Entity;
using DtoFw;
using DtoFw.Query;
using DtoFw.Result;
using Repository.Base;
using System.Text;
using Util;

namespace Repository
{
    public class MainMenuRepository : BaseRepository<MainMenuEntity>
    {
        private readonly SnowFlakeGenerator worker;
        public MainMenuRepository(SnowFlakeGenerator snowFlakeGenerator) : base(snowFlakeGenerator)
        {
            this.worker = snowFlakeGenerator;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public Pager<RoleMenuResult> QuerPageMenuList(MenuQuery query)
        {
            StringBuilder builder = new StringBuilder(@"SELECT IsLink,IsHide,IsKeepAlive,IsAffix,LinkPath,Auth,Title,Component,Id,ParentName,Sort,MenuName Name,MenuUrl Path,CreateDate,Level,CAST(ParentId AS UNSIGNED INTEGER)ParentId,Icon,Type from menu_view where Type=" + query.MenuType + "  ");
            if (!string.IsNullOrEmpty(query.ParentName))
                builder.Append(" AND ParentName like '" + query.ParentName + "%'");
            if (!string.IsNullOrEmpty(query.Name))
                builder.Append(" AND MenuName like '" + query.Name + "%'");
            if (!string.IsNullOrEmpty(query.Level))
                builder.Append(" AND Level=" + query.Level + "");
            Pager<RoleMenuResult> result = new Pager<RoleMenuResult>();
            result.TotalCount = base.GetDb().SqlQueryable<TotalResult>(string.Format("{0}", builder)).Count();
            string querySql = string.Format("{0} order by CreateDate DESC LIMIT " + (query.Page - 1) * query.PageSize + "," + query.PageSize + "", builder);
            result.Data = base.GetDb().SqlQueryable<RoleMenuResult>(querySql)?.ToList();
            return result;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public Pager<MenuInfoResult> QuerPageList(MenuQuery query)
        {
            StringBuilder builder = new StringBuilder(@"SELECT IsLink,IsHide,IsKeepAlive,IsAffix,LinkPath,Auth, Title,Component,Id,ParentName,Sort,MenuName Name,MenuUrl Path,CreateDate,Level,CAST(ParentId AS UNSIGNED INTEGER)ParentId,Icon,Type from menu_view where Type=" + query.MenuType);
            if (!string.IsNullOrEmpty(query.ParentName))
                builder.Append(" AND ParentName like '" + query.ParentName + "%'");
            if (!string.IsNullOrEmpty(query.Name))
                builder.Append(" AND MenuName like '" + query.Name + "%'");
            if (!string.IsNullOrEmpty(query.Level))
                builder.Append(" AND Level=" + query.Level + "");
            Pager<MenuInfoResult> result = new Pager<MenuInfoResult>();
            result.TotalCount = this.GetDb().SqlQueryable<TotalResult>(string.Format("{0}", builder)).Count();
            string querySql = string.Format("{0} order by CreateDate DESC LIMIT " + (query.Page - 1) * query.PageSize + "," + query.PageSize + "", builder);
            result.Data = this.GetDb().SqlQueryable<MenuInfoResult>(querySql)?.ToList();
            return result;
        }
    }
}
